package com.huilian.iotbox.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.huilian.iotbox.data.service.RabbitSenderCommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Slf4j
@Service
public class RabbitSenderCommonServiceImpl implements RabbitSenderCommonService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void sendRetryDouYinCallbackProcessingFailure(JSONObject body) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("iotBox-douYin-retryCallbackProcessingFailure", "iotBox-douYin-retryCallbackProcessingFailure.retryCallbackProcessingFailure", body, correlationData);
    }

    //定义消息声明
    final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            String outTradeNo = correlationData.getId();
            if (!ack) {
                log.error("订单：{},发送失败", correlationData.getId());
                log.error("MQ订单发送失败，Confirm插入数据库");
            }
            log.info("订单号：{}MQ 发送成功！", outTradeNo);
        }
    };

    //定义消息返回
    final RabbitTemplate.ReturnCallback returnCallback = new RabbitTemplate.ReturnCallback() {
        @Override
        public void returnedMessage(org.springframework.amqp.core.Message message, int replyCode,
                                    String replyTest, String exchange, String routingKey) {
            System.err.println("return: " + exchange + ",routingKey:" + routingKey + ",replyCode: " + replyCode + ",replyTest:" + replyTest);
            log.error("订单：{},发送失败", message);
            log.error("MQ订单发送失败，Confirm插入数据库");
        }
    };

}
